﻿window.addEventListener("error", function (e) {
    alert(e.lineno)
    alert(e.message)
}, false);
(function () {
    window.FunctionCache = []
   // if (navigator.userAgent.toLowerCase().indexOf("lifeapp") == -1) return;

    window.setupWebViewJavascriptBridge = function (callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'https://__bridge_loaded__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function () { document.documentElement.removeChild(WVJBIframe) }, 0)
    }
    window.ClientApp = {
        _: {
            i: function () {
                Array.prototype.find = function (ckfunction) {
                    var re = null;
                    this.forEach(function (e) {
                        if (ckfunction(e)) {
                            re = e;
                            return re;
                        }
                    })
                    return re;
                }
                //获得方法的参数
                Function.prototype.funcname = "";
                Function.prototype.fid = "";
                ClientApp._.fn = new Array();
                for (var key in ClientApp) {
                    if (key != "_") {
                        ClientApp._.o[key] = ClientApp[key];
                        ClientApp[key] = function () {
                            var guid = ClientApp._.g();
                            var finishfunc = {
                                fid: guid,
                                funcname: arguments.callee.funcname,
                                Do: function (param) {
                                    if (window.ClientLifeApp) {
                                        if (param) {
                                            window.ClientLifeApp[this.funcname](this.fid, JSON.stringify(param));
                                        }
                                        else {
                                            window.ClientLifeApp[this.funcname](this.fid);
                                        }
                                    } else {
                                        var ch = { func: this, param: param };
                                        FunctionCache.push(ch);
                                    }
                                },
                                backfunction: null
                            }
                            ClientApp._.fn.push(finishfunc)
                            ClientApp._.o[arguments.callee.funcname].apply(finishfunc, arguments);
                        }
                        ClientApp[key].funcname = key;
                    }
                }

            },
            fs: function (fid) {
                //alert(fid)
                //找到相应方法
                var func = ClientApp._.fn.find(function (j) { return j.fid == fid });
                if (func != null) {
                    //如果回调函数不为空
                    if (func.backfunction != null) {
                        //获取储存在原生内存中的结果
                        var result = window.ClientLifeApp.GetFunctionFinishData(func.fid)
                        if (!result) result = JSON.stringify({ Msg: "未知错误", IsError: true });
                        func.backfunction.apply(window, [JSON.parse(result)])
                    }
                }
            },
            fn: [{
                fid: "xxxx-xxxx-xxxx-xxxx",//guid值
                backfunction: function (result) { },//该函数所存的参数
            }],
            g: function () {
                var nguid = "";
                for (var i = 1; i <= 32; i++) {
                    var n = Math.floor(Math.random() * 16.0).toString(16);
                    nguid += n;
                    if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                        nguid += "-";
                }
                nguid = nguid + "-" + new Date().getTime();
                return nguid
            },
            o: {},
            iosdata: [],
            iosb: null

        },
        login: function (param) {
            this.backfunction = function (result) {
                if (result.Token)
                    param.finish({ IsError: false, Token: result.Token, Phone: result.Phone });
                else
                    param.finish({ IsError: true, Token: null, Msg: result.Msg })
            };
            this.Do(param.parameter)
        },
        SignUp: function (param) {
            this.backfunction = function (result) {
                if (result)
                    param.finish(result);
                else
                    param.finish({ IsError: true,  Msg: "JsSdk出错" })
            };
            this.Do(param.parameter)
        },
        appPay: function (param) {
            this.backfunction = function (result) {
                if (result)
                    param.finish(result);
                else
                    param.finish({ IsError: true, Msg: "JsSdk出错" })
            };
            this.Do(param.parameter)
        }
    };
    ClientApp._.i();
    window.InitIos = function () {
        if (!window.ClientLifeApp) {

            window.ClientLifeApp = {
                GetFunctionFinishData: function (fid) {
                    var fob = ClientApp._.iosdata.find(function (p) { return p.fid == fid });
                    if (fob) { return fob.result; } else { return null; }
                }
            }
            for (var key in ClientApp) {
                if (key != "_") {
                    window.ClientLifeApp[key] = function (fid, param) {
                        var funckeys = arguments.callee.funcname;

                        ClientApp._.iosb.callHandler(funckeys, param, function (result) {
                            ClientApp._.iosdata.push({ fid: fid, result: result });
                            ClientApp._.fs(fid);
                        });
                    }
                    window.ClientLifeApp[key].funcname = key;
                }
            };
            FunctionCache.forEach(function (that) {
                that.func.Do.apply(that.func, [that.param])
            })
     
        }
    }
    if (navigator.userAgent.toLowerCase().indexOf("iphone") > -1) {
        function setupWebViewJavascriptBridge(callback) {
            if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
            if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
            window.WVJBCallbacks = [callback];
            var WVJBIframe = document.createElement('iframe');
            WVJBIframe.style.display = 'none';
            WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function () { document.documentElement.removeChild(WVJBIframe) }, 0)
        }
        setupWebViewJavascriptBridge(function (bridge) {
            ClientApp._.iosb = bridge
            window.InitIos();
        });
    }

})();